// pages/shopDetails/shopDetails.js
const util = require('../../utils/util.js')
const app = getApp();
const cloudUrl = app.globalData.cloudUrl;
import Toast from '../../dist/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cloudUrl:app.globalData.cloudUrl,
    menus:[
      {id:1,name:"联系商家",img:"service.png",txtColor:"#000000",param:""},
      {id:2,name:"购物车",img:"menu/menu_gwc.png",txtColor:"#000000",param:""}],
    //幻灯片
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    circular: false,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
    background: ["shop/img1.png","shop/img2.png"],
    indexStr:"1/3",
    //以下是内容
    isShowTag:true,
    shopName:"松阳味道黑香肠",
    price:68,
    oldPrice:70,
    tags:["花菇","松阳花菇"],
    youfeiStr:"运费 ￥5",
    kucunStr:"库存 96",
    detailsDesc:"品牌：松阳味道\n配料：猪肉\n净含量：300g\n保质期：6个月",
    preImgs:[cloudUrl+"shop/pre1.png", cloudUrl+"shop/pre2.png"],
    curInfo:{},
    quickInfo:"",
    dialogPop:false,
    isTipsShow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    

    const info = JSON.parse(options.data);
    
    const youfeiStr = "邮费 ￥" + info.postage;
    const kucunStr = "库存 " + info.num;
    var desc = info.desc;
    desc = desc.replace(/<br>/g, "\n");
    //console.log("fdsfd desc=",desc)
    this.setData({background:info.swipers,curInfo:info,youfeiStr:youfeiStr,kucunStr:kucunStr,detailsDesc:desc});
    wx.setNavigationBarTitle({
      title: info.name,
    })

    const query = wx.createSelectorQuery()
    const q = query.select('#myimg')
    q.boundingClientRect()
    q.context()
    //q.fields()
    //q.node()
    //q.ref()
    q.scrollOffset()
    query.exec(function(res){
      console.log(res);
    })
  },
  onMenuClick(e){
    if(e.currentTarget.dataset.index == 0){
      this.setData({dialogPop:true});
    }
    else{
      util.gotoShopListPage([2,0]);
    }
  },
  onSwiperChange(e){
    //console.log("幻灯片滑动了。" + e.detail.current)
    const index = e.detail.current + 1;
    this.setData({
      indexStr:index+"/"+this.data.background.length
    })
  },
  onQuickClick(e){
    var info = this.data.curInfo;
    info = JSON.stringify(info);
    //console.log("shopDetails.js,",info);
    this.setData({quickInfo:info});
    this.setData({isTipsShow:!this.data.isTipsShow});
  },
  onNotesEvent(e){
    //console.log(e);
    if(e.detail.type == "close"){
      this.setData({isTipsShow:false});
    }
    else if(e.detail.type == "confirm"){
      if(e.detail.select && e.detail.num > 0){
        this.setData({isTipsShow:false});
        //do something...
        
        // 存入本地缓存
        const app = getApp();
        const _this = this;
        const key = "shopcart_" + app.globalData.openid;
        var arr = null;
        wx.getStorage({
          key: key,
          complete(res) {
            console.log("get storage,",res);
            arr = res.data;
            if(!arr){
              arr = [];
            }
            //console.log(e);
            const item = e.detail.item;
            item.buyNum = e.detail.num;
            arr.push(item);

            //插入
            wx.setStorage({
              key: key,
              data: arr,
              success() {
                Toast("加入购物车成功!");
              }
            })
          }
        })
      }
      else{
        Toast("请选择商品规格和属性");
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})